package com.cxyd.modules.wcs.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.constant.Constants;
import com.cxyd.common.constant.OperateConstants;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.wcs.dto.AgvCallbackDTO;
import com.cxyd.modules.wcs.dto.AgvTrkDTO;
import com.cxyd.modules.wcs.entity.AgvTrkEntity;
import com.cxyd.modules.wcs.entity.PostWmsEntity;
import com.cxyd.modules.wcs.entity.TrkEntity;
import com.cxyd.modules.wcs.service.AgvCallbackService;
import com.cxyd.modules.wcs.service.AgvTrkService;
import com.cxyd.modules.wcs.service.PostWmsService;
import com.cxyd.modules.wcs.service.TrkService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("wcs/agv-trk")
public class AgvTrkController {
    @Resource
    private AgvTrkService agvTrkService;
    @Resource
    private TrkService trkService;
    @Resource
    private PostWmsService postWmsService;

    @GetMapping("page")
    @RequiresPermissions("wcs:agv-trk:page")
    public Result<PageData<AgvTrkDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<AgvTrkDTO> page = agvTrkService.page(params);

        return new Result<PageData<AgvTrkDTO>>().ok(page);
    }

    @PostMapping("/operate")
    @LogOperation("人为操作")
    @RequiresPermissions("wcs:agv-trk:operate")
    public Result manualOperate(@RequestParam Map<String, Object> params) {
        String id = (String) params.get("id");
        String contNo = (String) params.get("contNo");
        String operateType = (String) params.get("operateType");
        if (OperateConstants.TASK_FORCE_DONE_OPERATE.equals(operateType)) {
            //如果是强制完成则调用callback的完成过程
            AgvTrkEntity entity = agvTrkService.getAgvTrkByContNo(contNo);
            //更新任务状态=4
            entity.setStatus(4);
            entity.setUpdateDate(new Date());
            agvTrkService.updateById(entity);
            //更新trk状态=2
            TrkEntity trkEntity = trkService.selectByContNo(contNo);
            trkEntity.setTrkSt(2);
            trkEntity.setUpdateDate(new Date());
            trkService.updateById(trkEntity);
            //上传POST
            PostWmsEntity postWms = new PostWmsEntity();
            postWms.setRequestPk(trkEntity.getRequestPk());
            postWms.setContNo(trkEntity.getContNo());
            postWms.setCurPos(trkEntity.getToPos());
//        类型为[完成]
            postWms.setNoticeType("2");
            postWms.setCode("0");
            postWms.setResult("success");
            postWms.setPostSt(0);
            postWms.setCreator(1002L);
            postWms.setUpdateDate(new Date());
            postWmsService.insert(postWms);
        }
        ProcResult procResult = agvTrkService.agvTrkOperate(params);
        if (Constants.SUCCESS.equals(procResult.getCode())) {
            return new Result().ok(procResult.getMsg());
        }
        return new Result().error(procResult.getMsg());
    }
}
